package com.linewell.system.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.linewell.commom.dao.impl.BaseDaoImpl;
import com.linewell.system.dao.ISysCustomerDao;
import com.linewell.system.model.SysCustomer;
import com.linewell.system.model.SysDepartment;
import com.linewell.system.model.SysRoles;
/**
 * 具体实现SysCustomer的CRUD
 * 
 * */
@Repository
public class SysCustomerDaoImpl extends BaseDaoImpl<SysCustomer> implements ISysCustomerDao {

	@Override
	public boolean deleteAll(String ids) {
		String hql = "delete SysCustomer s where s.custId in ("+ids+") ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	@Override
	public boolean removeAll(String ids) {
		String hql = "update SysCustomer s set s.status = 0 where s.custId in ("+ids+") ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysDepartment> getDep() {
		String hql = " from SysDepartment ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return  query.list();
	}

	@Override
	public boolean updateAll(SysCustomer c) {
		
		StringBuffer hql = new StringBuffer(
				"update SysCustomer s set ");
		
		//拼接修改条件
		if(c.getCustName() != null && !"".equals(c.getCustName())){
			hql.append("  s.custName = '"+c.getCustName()+"' ");
		}
		if(c.getSysDepartment().getDepId() != null && !"".equals(c.getSysDepartment().getDepId())){
			hql.append(" , s.sysDepartment.depId = "+c.getSysDepartment().getDepId()+" ");
		}
		if(c.getPhone() != null ){
			hql.append(" , s.phone = '"+c.getPhone()+"' ");
		}
		if(c.getStatus() != null && !"".equals(c.getStatus())){
			hql.append(" , s.status = "+c.getStatus()+" ");
		}

		hql.append(" where s.custId ="+c.getCustId()+" ");
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
		return (query.executeUpdate() > 0);
	}

}
